package org.vaadinpa.logic.commands;

import org.vaadinpa.Conf;
import org.vaadinpa.data.NewsItem;
import org.vaadinpa.data.Planet;
import org.vaadinpa.data.Resource;
import org.vaadinpa.data.TechLevel;
import org.vaadinpa.logic.Command;

public class AsteroidScanCommand extends Command {
	public final int num;

	public AsteroidScanCommand(Planet from, int num) {
		super(from);

		this.num = num;

		// withdraw scans
		planet.availableRoidScans -= num;
	}

	@Override
	public void execute() {

		// find roids
		double successRate = 0;

		switch (planet.level) {
		case LOWEST:
			successRate = (100 - planet.getTotalRoids()) / 100.0;
			double min = Conf.ROID_FINDING_RATE_LEVEL[TechLevel.LOWEST.ordinal()];
			successRate = successRate < min ? min : successRate;
			break;
		case LOW:
			successRate = (80 - planet.getTotalRoids()) / 100.0;
			min = Conf.ROID_FINDING_RATE_LEVEL[TechLevel.LOW.ordinal()];
			successRate = successRate < min ? min : successRate;
			break;
		case MODERATE:
		case HIGH:
		case HIGHEST:
			successRate = Conf.ROID_FINDING_RATE_LEVEL[planet.level.ordinal()];
			break;
		}

		int numRoids = (int) (successRate * num);

		// add roids
		planet.addRoids(Resource.NEW, numRoids);

		NewsItem i = new NewsItem("Asteroid Scan Excecuted", "Found " + numRoids + " new Asteroids.");
		planet.addNews(i);
	}

	@Override
	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void cancel() {
		// return scans
		planet.availableRoidScans += num;
	}

	@Override
	public String getDescription() {
		return "Using " + num + " scans";
	}

	@Override
	public String getName() {
		return "Asteroid scan";
	}

	@Override
	public void serialize(StringBuilder sb) {
		super.serializeBase(sb);

		sb.append("<num>" + num + "</num>");
		sb.append("</" + this.getClass().getSimpleName() + ">");
	}
}
